<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of article
 *
 * @author Dmitry
 */
class Models_Article extends Access_Db {
  public function getArticle($id)
  {
    $select = array(
        "id",
        "name",
        "content",
        "modified",
        "menu_id"
    );
    $this->select($select, "articles", "visible='1' and menu_id=$id");
    $row = $this->getSelected();
    return $row;
  }

  public function getArticleEdit($id)
  {
    $select = array(
        "id",
        "name",
        "content",
        "modified",
        "menu_id"
    );
    $this->select($select, "articles", "menu_id=$id");
    $row = $this->getSelected();
    return $row;
  }
  
  public function getNews()
  {
    $select = array(
        "id",
        "name",
        "modified",
        "menu_id"
    );
    $news = array();
    $this->select($select, "articles", "news='1' and visible='1' order by modified desc limit 0,10");
    while ($row = $this->getSelected())
    {
      $news[] = $row;
    }
    return $news;
  }
  
  public function addArticle($arr)
  {
      $this->prepare("INSERT INTO articles (name, content, menu_id, visible, news, menu)
                      VALUES (?, ?, ?, ?, ?, ?)");
      $this->insert($arr);
      $id = $this->getLAstInsertedID();
      $this->update("articles", "created=NOW(), modified=NOW()", "id=$id");
  }

  public function editArticle($arr)
  {
      $this->update("articles", "modified=NOW(),
                                 name='" . $arr[0] . "',
                                 content='" . $arr[1] . "',
                                 menu_id='" . $arr[2] . "',
                                 visible='" . $arr[3] . "',
                                 news='" . $arr[4] . "',
                                 menu='" . $arr[5] . "'", 'menu_id=' . $arr[6]);
  }
  
  function search($key)
  {
      if(strlen($key) < 7)
      {
          echo '<div class="alert alert-danger">
            <h4>Запит повинен складатися мінімум з чотирьох символів.</h4></div>';
          return array();
      }
      $words = array();
      $words = explode(' ',$key);
      $select = array(
          'id',
          'name',
          'modified',
          'menu_id',
          'content'
      );
      $table = 'articles';
      $where = '';
      foreach ($words as $word)
      {
          $where .= "name LIKE '%$word%' or content LIKE '%$word%' or ";
      }
      $where = ereg_replace(' or $', '', $where);
      $result = array();
      $this->select($select, $table, $where);
      while ($row = $this->getSelected())
      {
          $result[] = $row;
      }
      return $result;
  }
  
  function getMiniature($string, $length)
    {
        if(strlen($string) <= $length)
        {
            return $string;
        }
        $result = substr($string, 0,strpos($string, " ", $length));
        $result .= '...';
        $result = htmlspecialchars_decode($result);
        $result = strip_tags($result);
        return $result;
    }
}